package com.bittech.wiki_learn.service;

import com.bittech.wiki_learn.domain.Ebook;
import com.bittech.wiki_learn.domain.EbookExample;
import com.bittech.wiki_learn.mapper.EbookMapper;
import com.bittech.wiki_learn.req.EbookReq;
import com.bittech.wiki_learn.resp.EbookResp;
import com.bittech.wiki_learn.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EbookService {
    @Resource
    private EbookMapper ebookMapper;

    public List<EbookResp> queryBookByName(EbookReq ebookReq) {
        // 条件查询的模板,组装example
        EbookExample example = new EbookExample();
        EbookExample.Criteria criteria = example.createCriteria();
        // 动态SQL，当传入的getName不为空时才做like处理
        if(!ObjectUtils.isEmpty(ebookReq.getName())) {
            criteria.andNameLike("%" +ebookReq.getName() + "%");
        }
        List<Ebook> ebookList = ebookMapper.selectByExample(example);
        List<EbookResp> ebookResps = CopyUtil.copyList(ebookList,EbookResp.class);
        return ebookResps;
    }
}
